package com.xiaotao.user.controller;


import com.xiaotao.user.domain.dto.PageDTO;
import com.xiaotao.user.domain.dto.ProductDTO;
import com.xiaotao.user.domain.query.PageQuery;
import com.xiaotao.user.domain.vo.ProductListVO;
import com.xiaotao.user.service.IProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class ProductController {

    final IProductService productService;

    // 新增商品
    @PostMapping("/add")
    public void addProduct(@RequestBody ProductDTO dto){
        productService.addProduct(dto);
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
    }

    // 查询是否有这条商品 根据商品id查询，有返回1，无返回0
    @GetMapping("/{id}")
    public String selectProductById(@PathVariable Long id){
        return productService.selectProductById(id) + "";
    }


    //测试接口
    @GetMapping("/test")
    public String test() {
        return "Backend is running!";
    }

    // 获取全部的商品列表
    @GetMapping
    public List<ProductListVO> getProducts() {
        return productService.getProducts();
    }

    // 分页获取商品列表
    @PostMapping("/page")
    public PageDTO<ProductListVO> getProductsByPageDTO(@Validated @RequestBody PageQuery query){
        return productService.getProductsByPageDTO(query);
    }



}
